package Java7_Concurrency_Cookbook.thread_synchronized_helper.semaphore;

import java.util.concurrent.Semaphore;

public class PrintQueue {
    private final Semaphore semaphore;

    public PrintQueue() {
        this.semaphore = new Semaphore(1, true);
    }

    public void printJob(Object document) {
        try {
            semaphore.acquire();
            double duration = Math.random() * 1000;
            String tName = Thread.currentThread().getName();
            System.out.println(tName + " : PrintQueue : Printing a Job during " + duration);
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            semaphore.release();
        }
    }
}
